package com.api.demo.util;

import com.alibaba.fastjson.JSON;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * @description: 签名工具
 * @author: yanming
 * @createDate: 2022/12/26 13:58
 */
public class ApiSignUtil {



        public static void main(String[] args) {
            Map<String, Object> paramMap = null;
            String sign = null;


            String s = new String("{\"bankCode\":\"123594567164\",\"bankName\":\"测试\",\"businessLicense\":\"https://oss.hui7z.com/1003/2022/12/02e0e634-59be-499f-a76a-30575a6de11c.pdf\",\"businessScope\":\"123\",\"companyMan\":\"法定代表人姓名\",\"companyName\":\"关联企业2\",\"createDate\":\"2022-11-24T18:38:29\",\"creditCode\":\"999999\",\"idCardBack\":\"https://oss.hui7z.com/1003/2022/12/02e0e634-59be-499f-a76a-30575a6de11c.pdf\",\"idCardCode\":\"350781196403075044\",\"idCardFront\":\"https://oss.hui7z.com/1003/2022/12/02e0e634-59be-499f-a76a-30575a6de11c.pdf\",\"industry\":\"行业名称-1.2\",\"linkEmail\":\"13966664444@163.com\",\"linkMan\":\"业务联系人\",\"linkMobile\":\"13966664444\",\"mobile\":\"13966664444\",\"sign\":\"34D6F3BCE9047FCF2E9210033B310798\",\"timestamp\":1670929587195}");

            String s12 = "{\"bankCode\":\"123594567164\",\"bankName\":\"测试\",\"businessLicense\":\"https://oss.hui7z.com/1003/2022/12/02e0e634-59be-499f-a76a-30575a6de11c.pdf\",\"businessScope\":\"123\",\"companyMan\":\"法定代表人姓名\",\"companyName\":\"关联企业2\",\"createDate\":\"2022-11-24 18:38:29\",\"creditCode\":\"999999\",\"idCardBack\":\"https://oss.hui7z.com/1003/2022/12/02e0e634-59be-499f-a76a-30575a6de11c.pdf\",\"idCardCode\":\"350781196403075044\",\"idCardFront\":\"https://oss.hui7z.com/1003/2022/12/02e0e634-59be-499f-a76a-30575a6de11c.pdf\",\"industry\":\"行业名称-1.2\",\"linkEmail\":\"13966664444@163.com\",\"linkMan\":\"业务联系人\",\"linkMobile\":\"13966664444\",\"mobile\":\"13966664444\",\"timestamp\":1670930200051}";
//        ,"sign":"34116D6719345E3D2B1F205F8834F268"
//        String s12 = "{\"bankCode\":\"123594567164\",\"bankName\":\"测试\",\"businessLicense\":\"https://oss.hui7z.com/1003/2022/12/02e0e634-59be-499f-a76a-30575a6de11c.pdf\",\"businessScope\":\"123\",\"companyMan\":\"法定代表人姓名\",\"companyName\":\"关联企业2\",\"createDate\":\"2022-11-24 18:38:29\",\"creditCode\":\"999999\",\"idCardBack\":\"https://oss.hui7z.com/1003/2022/12/02e0e634-59be-499f-a76a-30575a6de11c.pdf\",\"idCardCode\":\"350781196403075044\",\"idCardFront\":\"https://oss.hui7z.com/1003/2022/12/02e0e634-59be-499f-a76a-30575a6de11c.pdf\",\"industry\":\"行业名称-1.2\",\"linkEmail\":\"13966664444@163.com\",\"linkMan\":\"业务联系人\",\"linkMobile\":\"13966664444\",\"mobile\":\"13966664444\",\"sign\":\"34116D6719345E3D2B1F205F8834F268\",\"timestamp\":1670930200051}";

//        JSON.parseObject(SyncFangChengCompanyDTO.c)


//        {"bankCode":"123594567164","bankName":"测试","businessLicense":"https://oss.hui7z.com/1003/2022/12/02e0e634-59be-499f-a76a-30575a6de11c.pdf","businessScope":"123","companyMan":"法定代表人姓名","companyName":"关联企业2","createDate":"2022-11-24T18:38:29","creditCode":"999999","idCardBack":"https://oss.hui7z.com/1003/2022/12/02e0e634-59be-499f-a76a-30575a6de11c.pdf","idCardCode":"350781196403075044","idCardFront":"https://oss.hui7z.com/1003/2022/12/02e0e634-59be-499f-a76a-30575a6de11c.pdf","industry":"行业名称-1.2","linkEmail":"13966664444@163.com","linkMan":"业务联系人","linkMobile":"13966664444","mobile":"13966664444","timestamp":1670929587195}

            String s1 = JSON.toJSONString(s);
            System.out.println(s1);
            Map<String,Object> map = (Map<String,Object>) JSON.parseObject(s12, Map.class);


            String sign1 = getSign(map);
            System.out.println("s1:" +sign1);
            String s22 = "appid=20221025104200001&bankCode=123594567164&bankName=测试&businessLicense=https://oss.hui7z.com/1003/2022/12/02e0e634-59be-499f-a76a-30575a6de11c.pdf&businessScope=123&companyMan=法定代表人姓名&companyName=关联企业2&createDate=2022-11-24T18:38:29&creditCode=999999&idCardBack=https://oss.hui7z.com/1003/2022/12/02e0e634-59be-499f-a76a-30575a6de11c.pdf&idCardCode=350781196403075044&idCardFront=https://oss.hui7z.com/1003/2022/12/02e0e634-59be-499f-a76a-30575a6de11c.pdf&industry=行业名称-1.2&key=FC1e5a0609aef34807b2034c9bee3eeeaf&linkEmail=13966664444@163.com&linkMan=业务联系人&linkMobile=13966664444&mobile=13966664444&timestamp=1670929587195";


            String s2 = DigestUtils.md5DigestAsHex(s22.getBytes()).toUpperCase();
            System.out.println(s2);

//        SignUtil signUtil = new SignUtil();
//        Boolean result = signUtil.signatureVerification(map, "2D56C56CCF00D504F3AD66FD4FD02F58");

            //全部参数加密示例
            //订单的需要排除订单明细（订单明细不参与签名）
//        SyncFangChengCompanyDTO syncFangChengCompanyDTO = new SyncFangChengCompanyDTO();
//        syncFangChengCompanyDTO.setCompanyId("12312312");
//        syncFangChengCompanyDTO.setCompanyMan("1231231231");
//        syncFangChengCompanyDTO.setCompanyProvince("21312312312312");
//        syncFangChengCompanyDTO.setBankName("321321312312");
//
//        Map<String, Object> paramMap = JSON.parseObject(JSON.toJSONString(syncFangChengCompanyDTO), new TypeReference<Map<String, Object>>() {
//        });
//        SignUtil signUtil = new SignUtil();
//        Boolean result = signUtil.signatureVerification(paramMap, "2D56C56CCF00D504F3AD66FD4FD02F58");
//        System.out.println(result);


            //加签举例
//        paramMap = new HashMap<>();
//        paramMap.put("agentId", 1404738560589172738L);
//        paramMap.put("timestamp", 21312312);
//        paramMap.put("key", "12345678ABC");
//        sign = getSign(JSON.parseObject(JSON.toJSONString(paramMap)));
//        System.out.println(sign);
//
//
//        //商户相关操作-----------------------------------------------------------
//
//        //创建商户
//        paramMap = new HashMap<>();
//        paramMap.put("agentId", 1404738560589172738L);
//        paramMap.put("timestamp", 21312312);
//        paramMap.put("key", "12345678ABC");
//        paramMap.put("creditCode", "312ds1dd121e12");
//        paramMap.put("companyName", "公司名称");
//        sign = getSign(JSON.parseObject(JSON.toJSONString(paramMap)));
//        //agentId=1404738560589172738&companyName=公司名称&creditCode=312ds1dd121e12&key=12345678ABC&timestamp=21312312
//        //6E97F79663C587F104FC1DC92FBA51E7
//        System.out.println(sign);
//
//
//
//        //申请合作
//        paramMap = new HashMap<>();
//        paramMap.put("agentId", 1404738560589172738L);
//        paramMap.put("timestamp", 21312312);
//        paramMap.put("key", "12345678ABC");
//        paramMap.put("creditCode", "312ds1dd121e12");
//        paramMap.put("taxId", "1231231231");
//        sign = getSign(JSON.parseObject(JSON.toJSONString(paramMap)));
//        //agentId=1404738560589172738&creditCode=312ds1dd121e12&key=12345678ABC&taxId=1231231231&timestamp=21312312
//        //5486F63E332339C360184984556DD104
//        System.out.println(sign);
//
//
////        //查询可合作的服务商列表
//        paramMap = new HashMap<>();
//        paramMap.put("agentId", 1404738560589172738L);
//        paramMap.put("timestamp", 21312312);
//        paramMap.put("key", "12345678ABC");
//        sign = getSign(JSON.parseObject(JSON.toJSONString(paramMap)));
//        //agentId=1404738560589172738&key=12345678ABC&timestamp=21312312
//        //940F00939C8DA4808D98E8186CDAC7F2
//        System.out.println(sign);
//
//        //查询审核状态
//        paramMap = new HashMap<>();
//        paramMap.put("agentId", 1404738560589172738L);
//        paramMap.put("timestamp", 21312312);
//        paramMap.put("key", "12345678ABC");
//        paramMap.put("creditCode", "312ds1dd121e12");
//        sign = getSign(JSON.parseObject(JSON.toJSONString(paramMap)));
//        //agentId=1404738560589172738&creditCode=312ds1dd121e12&key=12345678ABC&timestamp=21312312
//        //A36ED77F33C907FA71408E3F7A2BE032
//        System.out.println(sign);
//
//        //线下充值
//        paramMap = new HashMap<>();
//        paramMap.put("agentId", 1404738560589172738L);
//        paramMap.put("timestamp", 21312312);
//        paramMap.put("key", "12345678ABC");
//        paramMap.put("creditCode", "312ds1dd121e12");
//        paramMap.put("taxId", "1231231231");
//        sign = getSign(JSON.parseObject(JSON.toJSONString(paramMap)));
//        //agentId=1404738560589172738&creditCode=312ds1dd121e12&key=12345678ABC&taxId=1231231231&timestamp=21312312
//        //5486F63E332339C360184984556DD104
//        System.out.println(sign);
//
//        //查询账户信息
//        paramMap = new HashMap<>();
//        paramMap.put("agentId", 1404738560589172738L);
//        paramMap.put("timestamp", 21312312);
//        paramMap.put("key", "12345678ABC");
//        paramMap.put("creditCode", "312ds1dd121e12");
//        paramMap.put("taxId", "1231231231");
//        sign = getSign(JSON.parseObject(JSON.toJSONString(paramMap)));
//        //agentId=1404738560589172738&creditCode=312ds1dd121e12&key=12345678ABC&taxId=1231231231&timestamp=21312312
//        //5486F63E332339C360184984556DD104
//        System.out.println(sign);
//
//        //查询合作状态
//        paramMap = new HashMap<>();
//        paramMap.put("agentId", 1506923661431410690L);
//        paramMap.put("timestamp", "123123113123231");
//        paramMap.put("key", "123");
//        paramMap.put("creditCode", "asdfasdft133811");
//        paramMap.put("taxId", "1504714679979610114");
//        sign = getSign(JSON.parseObject(JSON.toJSONString(paramMap)));
//        //agentId=1404738560589172738&creditCode=312ds1dd121e12&key=12345678ABC&taxId=1231231231&timestamp=21312312
//        //5486F63E332339C360184984556DD104
//        System.out.println(sign);
//
//        //查询充值记录
//        paramMap = new HashMap<>();
//        paramMap.put("agentId", 1404738560589172738L);
//        paramMap.put("timestamp", 21312312);
//        paramMap.put("key", "12345678ABC");
//        paramMap.put("creditCode", "312ds1dd121e12");
//        paramMap.put("taxId", "1231231231");
//        sign = getSign(JSON.parseObject(JSON.toJSONString(paramMap)));
//        //agentId=1404738560589172738&creditCode=312ds1dd121e12&key=12345678ABC&taxId=1231231231&timestamp=21312312
//        //5486F63E332339C360184984556DD104
//        System.out.println(sign);
//
//        //商户相关操作-----------------------------------------------------------
//
//
//        //创客相关操作-----------------------------------------------------------
//
//        //添加创客
//        paramMap = new HashMap<>();
//        paramMap.put("agentId", 1404738560589172738L);
//        paramMap.put("timestamp", 21312312);
//        paramMap.put("key", "12345678ABC");
//        paramMap.put("creditCode", "312ds1dd121e12");
//        paramMap.put("taxId", "1231231231");
//        paramMap.put("idCardCode", "122222333333");
//        sign = getSign(JSON.parseObject(JSON.toJSONString(paramMap)));
//        //agentId=1404738560589172738&creditCode=312ds1dd121e12&idCardCode=122222333333&key=12345678ABC&taxId=1231231231&timestamp=21312312
//        //D31741F8044D08196C8F0CFD940B4792
//        System.out.println(sign);

            //查询创客合同
//        paramMap = new HashMap<>();
//        paramMap.put("agentId", "12312312312");
//        paramMap.put("timestamp", "1232131312321");
//        paramMap.put("creditCode", "213213123123");
//        paramMap.put("idCardCode", "2132131312312");
//        paramMap.put("key", "12345678ABC");


            //创客相关操作-----------------------------------------------------------


            //订单相关操作-----------------------------------------------------------

//        //创建订单
//        paramMap = new HashMap<>();
//        paramMap.put("agentId", 1404738560589172738L);
//        paramMap.put("timestamp", 21312312);
//        paramMap.put("key", "12345678ABC");
//        paramMap.put("creditCode", "312ds1dd121e12");
//        paramMap.put("taxId", "1231231231");
//        paramMap.put("taskName", "测试任务");
//        sign = getSign(JSON.parseObject(JSON.toJSONString(paramMap)));
//        //agentId=1404738560589172738&creditCode=312ds1dd121e12&key=12345678ABC&taskName=测试任务&taxId=1231231231&timestamp=21312312
//        //C8E0BF707AF341EB4408C8A27FA9E598
//        System.out.println(sign);
//
//        //订单支付
//        paramMap = new HashMap<>();
//        paramMap.put("agentId", 1404738560589172738L);
//        paramMap.put("timestamp", 21312312);
//        paramMap.put("key", "12345678ABC");
//        paramMap.put("id", "12312312312312312");
//        sign = getSign(JSON.parseObject(JSON.toJSONString(paramMap)));
//        //agentId=1404738560589172738&id=12312312312312312&key=12345678ABC&timestamp=21312312
//        //54FBB6D4CACA136CDF39673FF86BBCB6
//        System.out.println(sign);
//
//        //查询订单
//        paramMap = new HashMap<>();
//        paramMap.put("agentId", 1404738560589172738L);
//        paramMap.put("timestamp", 21312312);
//        paramMap.put("key", "12345678ABC");
//        paramMap.put("id", "12312312312312312");
//        sign = getSign(JSON.parseObject(JSON.toJSONString(paramMap)));
//        //agentId=1404738560589172738&id=12312312312312312&key=12345678ABC&timestamp=21312312
//        //54FBB6D4CACA136CDF39673FF86BBCB6
//        System.out.println(sign);
//
//
//        //查询订单任务类型
//        paramMap = new HashMap<>();
//        paramMap.put("agentId", 1404738560589172738L);
//        paramMap.put("timestamp", 21312312);
//        paramMap.put("key", "12345678ABC");
//        paramMap.put("taxId", "12312312312312312");
//        sign = getSign(JSON.parseObject(JSON.toJSONString(paramMap)));
//        //agentId=1404738560589172738&key=12345678ABC&taxId=12312312312312312&timestamp=21312312
//        //845E5DD1866D3A2D2E62FA9B46719DD2
//        System.out.println(sign);
////
//        //订单相关操作----------------------------------------------------------
//
//
//        //发票相关操作-----------------------------------------------------------
//        //查询申请开票信息
//        paramMap = new HashMap<>();
//        paramMap.put("agentId", 1404738560589172738L);
//        paramMap.put("timestamp", 21312312);
//        paramMap.put("key", "12345678ABC");
//        sign = getSign(JSON.parseObject(JSON.toJSONString(paramMap)));
//        //agentId=1404738560589172738&key=12345678ABC&timestamp=21312312
//        //940F00939C8DA4808D98E8186CDAC7F2
//        System.out.println(sign);
//
//        //申请开票
//        paramMap = new HashMap<>();
//        paramMap.put("agentId", 1404738560589172738L);
//        paramMap.put("timestamp", 21312312);
//        paramMap.put("key", "12345678ABC");
//        paramMap.put("id", "21313123213213");
//        paramMap.put("invoiceCatalogId", "1566998174684418050");
//        sign = getSign(JSON.parseObject(JSON.toJSONString(paramMap)));
//        //agentId=1404738560589172738&id=21313123213213&invoiceCatalogId=1566998174684418050&key=12345678ABC&timestamp=21312312
//        //75645C089030E0C30D36A93AAEB34B79
//        System.out.println(sign);
//
//        //查询地址ID
//        paramMap = new HashMap<>();
//        paramMap.put("agentId", 1404738560589172738L);
//        paramMap.put("timestamp", 21312312);
//        paramMap.put("key", "12345678ABC");
//        sign = getSign(JSON.parseObject(JSON.toJSONString(paramMap)));
//        //agentId=1404738560589172738&key=12345678ABC&timestamp=21312312
//        //940F00939C8DA4808D98E8186CDAC7F2
//        System.out.println(sign);
//
//
//        //查询开票需要的参数
//        paramMap = new HashMap<>();
//        paramMap.put("agentId", 1404738560589172738L);
//        paramMap.put("timestamp", 21312312);
//        paramMap.put("key", "12345678ABC");
//        sign = getSign(JSON.parseObject(JSON.toJSONString(paramMap)));
//        //agentId=1404738560589172738&key=12345678ABC&timestamp=21312312
//        //940F00939C8DA4808D98E8186CDAC7F2
//        System.out.println(sign);
//
//        //查询发票状态信息
//        paramMap = new HashMap<>();
//        paramMap.put("agentId", 1404738560589172738L);
//        paramMap.put("timestamp", 21312312);
//        paramMap.put("key", "12345678ABC");
//        sign = getSign(JSON.parseObject(JSON.toJSONString(paramMap)));
//        //agentId=1404738560589172738&key=12345678ABC&timestamp=21312312
//        //940F00939C8DA4808D98E8186CDAC7F2
//        System.out.println(sign);


            //发票相关操作-----------------------------------------------------------


        }

        /**
         * 获取签名
         *
         * @param paramMap 根据对应接口提供的加签参数提供
         * @return
         */
        public static String getSign(Map<String, Object> paramMap) {
            String text = getUrlText(paramMap);
            System.err.println(text);
            return DigestUtils.md5DigestAsHex(text.getBytes()).toUpperCase();
        }

        private static String getUrlText(Map<String, Object> beanMap) {
            beanMap = getSortedMap(beanMap);
            StringBuilder builder = new StringBuilder();
            for (String key : beanMap.keySet()) {
                String value = beanMap.get(key).toString();
                builder.append(key);
                builder.append('=');
                builder.append(value);
                builder.append('&');
            }
            String text = builder.toString();
            return text.substring(0, text.length() - 1);
        }

        public static Map<String, String> transMap(String str) {
            String[] key_value = str.split("&");
            Map<String, String> map = new HashMap<>();
            for (String e : key_value) {
                String[] value = e.split("=");
                if (value.length != 2) {
                    continue;
                }
                map.put(value[0], value[1]);
            }
            return map;
        }

        /**
         * 对普通map进行排序
         *
         * @param paramMap
         * @return
         */
        private static Map<String, Object> getSortedMap(Map<String, Object> paramMap) {
            SortedMap<String, Object> map = new TreeMap<>();
            for (String key : paramMap.keySet()) {
                if (key != null) {
                    Object value = paramMap.get(key);
                    if (value != null) {
                        String valueStr = String.valueOf(value);
                        if (valueStr != null && !"".equals(valueStr)) {
                            map.put(key, value);
                        }
                    }
                }
            }
            return map;
        }

        public static String getUrlParams(Map<String, Object> beanMap) {
            StringBuilder builder = new StringBuilder();
            for (String key : beanMap.keySet()) {
                String value = "";
                if (beanMap.get(key) != null) {
                    value = beanMap.get(key).toString();
                }
                builder.append(key);
                builder.append('=');
                builder.append(value);
                builder.append('&');
            }
            String text = builder.toString();
            return text.substring(0, text.length() - 1);
        }


        public Boolean signatureVerification(Map<String, Object> paramMap, String sign) {
            String sign1 = getSign(JSON.parseObject(JSON.toJSONString(paramMap)));
            System.err.println(sign1);
            if (null == sign1 || !sign.equals(sign1)) {
                return false;
            }
            return true;
        }



}